import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'

import './assets/styles/border.css'
import './assets/styles/reset.css'
import './assets/styles/iconfont.css'
import './assets/styles/zy.media.min.css'

// 导入vant
import Vant from 'vant'
import 'vant/lib/index.css'
import {
  Toast
} from 'vant'
Vue.prototype.$msg = Toast

// 导入NProgress进度条
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'


import axios from 'axios'
//配置请求的根路径
axios.defaults.baseURL = 'http://112.74.99.5:3000/web/api'
// axios请求拦截  在request中 展示进度条
axios.interceptors.request.use(config => {
  NProgress.start()
  // localStoragese：常用于长期登录（+判断用户是否已登录），适合长期保存在本地的数据。sessionStorage：敏感账号一次性登录；
  if (window.sessionStorage.getItem('token') || window.sessionStorage.getItem('id')) {
    // 为请求头对象，添加 Token 验证的 Authorization 字段
    config.headers.Authorization = 'Bearer ' + window.sessionStorage.getItem('token')
  }
  // 必须return
  return config
})
//在response中 隐藏进度条
axios.interceptors.response.use(config => {
    NProgress.done()
    return config
  }, error => {
    if (error.response.status == 401 || error.response.status == 402) {
      router.push('/login')
      Vue.prototype.$msg.fail(error.response.data.message)
    }
    return Promise.reject(error)
  }

)
Vue.prototype.$axios = axios

//日期过滤器 
// 使用时: <template slot-scope="scope">
// {{ scope.row.*** | format('yyyy-MM-dd hh:mm:ss') }}
// </template> 即可
Vue.filter('format', function (val, date1) {
  function dateFormat(date, format) {
    if (typeof date === "string") {
      var mts = date.match(/(\/Date\((\d+)\)\/)/);
      if (mts && mts.length >= 3) {
        date = parseInt(mts[2]);
      }
    }
    date = new Date(date);
    if (!date || date.toUTCString() == "Invalid Date") {
      return "";
    }
    var map = {
      "M": date.getMonth() + 1, //月份 
      "d": date.getDate(), //日 
      "h": date.getHours(), //小时 
      "m": date.getMinutes(), //分 
      "s": date.getSeconds(), //秒 
      "q": Math.floor((date.getMonth() + 3) / 3), //季度 
      "S": date.getMilliseconds() //毫秒 
    };

    format = format.replace(/([yMdhmsqS])+/g, function (all, t) {
      var v = map[t];
      if (v !== undefined) {
        if (all.length > 1) {
          v = '0' + v;
          v = v.substr(v.length - 2);
        }
        return v;
      } else if (t === 'y') {
        return (date.getFullYear() + '').substr(4 - all.length);
      }
      return all;
    });
    return format;
  }
  return dateFormat(val, date1)
})

Vue.use(Vant)
Vue.config.productionTip = false

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
